<?php

/**
 * Vehicle filter form base class.
 *
 * @package    symfony
 * @subpackage filter
 * @author     Bo Wang < beau32 [at] gmail.com >
 * @version    SVN: $Id: sfDoctrineFormFilterGeneratedTemplate.php 24171 2009-11-19 16:37:50Z Kris.Wallsmith $
 */
abstract class BaseVehicleFormFilter extends BaseFormFilterDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'make_id'              => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Make'), 'add_empty' => true)),
      'model_id'             => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Model'), 'add_empty' => true)),
      'series'               => new sfWidgetFormFilterInput(),
      'body_style_id'        => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('BodyStyle'), 'add_empty' => true)),
      'year'                 => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'transmission'         => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'engine'               => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'fuel'                 => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'exterior_color'       => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'doors'                => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'cylinders'            => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'mileage'              => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'stereo_description'   => new sfWidgetFormFilterInput(),
      'description'          => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'stock_id'             => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'price'                => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'view'                 => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'is_featured'          => new sfWidgetFormChoice(array('choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no'))),
      'is_special'           => new sfWidgetFormChoice(array('choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no'))),
      'wof_expires'          => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'registration_expires' => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'plate'                => new sfWidgetFormFilterInput(),
      'created_at'           => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => false)),
      'updated_at'           => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => false)),
      'features_list'        => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Feature')),
    ));

    $this->setValidators(array(
      'make_id'              => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('Make'), 'column' => 'id')),
      'model_id'             => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('Model'), 'column' => 'id')),
      'series'               => new sfValidatorPass(array('required' => false)),
      'body_style_id'        => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('BodyStyle'), 'column' => 'id')),
      'year'                 => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'transmission'         => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'engine'               => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'fuel'                 => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'exterior_color'       => new sfValidatorPass(array('required' => false)),
      'doors'                => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'cylinders'            => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'mileage'              => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'stereo_description'   => new sfValidatorPass(array('required' => false)),
      'description'          => new sfValidatorPass(array('required' => false)),
      'stock_id'             => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'price'                => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'view'                 => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'is_featured'          => new sfValidatorChoice(array('required' => false, 'choices' => array('', 1, 0))),
      'is_special'           => new sfValidatorChoice(array('required' => false, 'choices' => array('', 1, 0))),
      'wof_expires'          => new sfValidatorPass(array('required' => false)),
      'registration_expires' => new sfValidatorPass(array('required' => false)),
      'plate'                => new sfValidatorPass(array('required' => false)),
      'created_at'           => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59')))),
      'updated_at'           => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59')))),
      'features_list'        => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Feature', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('vehicle_filters[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function addFeaturesListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query->leftJoin('r.VehicleFeature VehicleFeature')
          ->andWhereIn('VehicleFeature.feature_id', $values);
  }

  public function getModelName()
  {
    return 'Vehicle';
  }

  public function getFields()
  {
    return array(
      'id'                   => 'Number',
      'make_id'              => 'ForeignKey',
      'model_id'             => 'ForeignKey',
      'series'               => 'Text',
      'body_style_id'        => 'ForeignKey',
      'year'                 => 'Number',
      'transmission'         => 'Number',
      'engine'               => 'Number',
      'fuel'                 => 'Number',
      'exterior_color'       => 'Text',
      'doors'                => 'Number',
      'cylinders'            => 'Number',
      'mileage'              => 'Number',
      'stereo_description'   => 'Text',
      'description'          => 'Text',
      'stock_id'             => 'Number',
      'price'                => 'Number',
      'view'                 => 'Number',
      'is_featured'          => 'Boolean',
      'is_special'           => 'Boolean',
      'wof_expires'          => 'Text',
      'registration_expires' => 'Text',
      'plate'                => 'Text',
      'created_at'           => 'Date',
      'updated_at'           => 'Date',
      'features_list'        => 'ManyKey',
    );
  }
}
